package cn.itsource.pet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.LoginInfoUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.pet.domain.SearchMasterMessage;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.SearchMasterMessageMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.SearchMasterMessageQuery;
import cn.itsource.pet.service.ISearchMasterMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
//Propagation.SUPPORTS:支持别人事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class SearchMasterMessageServiceImpl extends BaseServiceImpl<SearchMasterMessage, SearchMasterMessageQuery> implements ISearchMasterMessageService {

    @Autowired
    private SearchMasterMessageMapper messageMapper;

    @Autowired
    private SearchMasterMsgMapper msgMapper;

    @Override
    public AjaxResult publish(SearchMasterMessage message) {

        //入库到审核表
        Long userId = LoginInfoUtil.getUserId();
        message.setUserId(userId);

        messageMapper.insertSelective(message);

        return AjaxResult.createSuccess();
    }

    @Override
    public AjaxResult shengHe(SearchMasterMessage message) {

        //查询一把数据库,如果message的状态不是0 ,就返回"你的技术还可以,但是还不够" 扩展

        //驳回
        if (message.getState() == -1){
            SearchMasterMessage smm = new SearchMasterMessage();
            smm.setState(message.getState());
            smm.setId(message.getId());
            //修改状态
            messageMapper.updateByPrimaryKeySelective(smm);
            return AjaxResult.createSuccess("驳回成功");
        }

        //审核通过进入接单表
        //1.先根据id查询到审核表的数据
        SearchMasterMessage searchMasterMessage = messageMapper.selectByPrimaryKey(message.getId());

        //1.1修改审核表的状态
        searchMasterMessage.setState(message.getState());
        messageMapper.updateByPrimaryKeySelective(searchMasterMessage);

        //2.拷贝数据
        SearchMasterMsg searchMasterMsg = BeanUtil.copyProperties(searchMasterMessage, SearchMasterMsg.class);
        searchMasterMsg.setState(0);//未接单
        searchMasterMsg.setId(null);
        searchMasterMsg.setNote(message.getNote());

        //3.新增接单表数据
        msgMapper.insertSelective(searchMasterMsg);

        //返回
        return AjaxResult.createSuccess("审核完毕");
    }

    @Override
    public PageList<SearchMasterMessage> findPage(SearchMasterMessageQuery query) {
        //1.分页查询
        PageHelper.startPage(query.getPage(),query.getSize());
        //2.去xml查询数据
        List<SearchMasterMessage> list = messageMapper.findPage(query);
        //3.封装
        PageInfo<SearchMasterMessage> pageInfo = new PageInfo<>(list);
        //4.返回自定义分页对象
        return new PageList<>(pageInfo.getTotal(),pageInfo.getList());
    }
}
